package com.wutongyu.mannyburger;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class UserOrderAdapter extends RecyclerView.Adapter<UserOrderAdapter.OrderViewHolder> {

    private List<Order> orderList;
    private OnItemClickListener onItemClickListener;

    // 定义点击事件接口
    public interface OnItemClickListener {
        void onItemClick(Order order);
    }

    // 设置点击事件监听器
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    public UserOrderAdapter(List<Order> orderList) {
        this.orderList = orderList;
    }

    @NonNull
    @Override
    public OrderViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_user_order, parent, false);
        return new OrderViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull OrderViewHolder holder, int position) {
        Order order = orderList.get(position);
        
        // 设置订单ID
        holder.textViewOrderId.setText("订单号: " + order.getId());
        
        // 设置订单商品
        holder.textViewOrderItems.setText(order.getItems());
        
        // 设置备注
        String note = order.getNote();
        if (note != null && !note.isEmpty()) {
            holder.textViewOrderNote.setText(note);
        } else {
            holder.textViewOrderNote.setText("无备注");
        }
        
        // 设置价格
        holder.textViewOrderPrice.setText(String.format("¥%.2f", order.getTotalPrice()));
        
        // 设置送餐方式
        String deliveryType = order.getDeliveryType();
        if (deliveryType == null || deliveryType.isEmpty()) {
            deliveryType = "自提"; // 默认为自提
        }
        holder.textViewDeliveryType.setText(deliveryType);
        
        // 设置送餐地址
        String deliveryAddress = order.getDeliveryAddress();
        if (deliveryType.equals("外卖") && !TextUtils.isEmpty(deliveryAddress)) {
            holder.textViewDeliveryAddress.setText(deliveryAddress);
            holder.textViewDeliveryAddress.setVisibility(View.VISIBLE);
        } else {
            holder.textViewDeliveryAddress.setVisibility(View.GONE);
        }
        
        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(order);
            }
        });
    }

    @Override
    public int getItemCount() {
        return orderList != null ? orderList.size() : 0;
    }

    public void updateData(List<Order> newOrderList) {
        this.orderList = newOrderList;
        notifyDataSetChanged();
    }

    static class OrderViewHolder extends RecyclerView.ViewHolder {
        TextView textViewOrderId;
        TextView textViewOrderItems;
        TextView textViewOrderNote;
        TextView textViewOrderPrice;
        TextView textViewDeliveryType;
        TextView textViewDeliveryAddress;

        public OrderViewHolder(@NonNull View itemView) {
            super(itemView);
            textViewOrderId = itemView.findViewById(R.id.text_view_order_id);
            textViewOrderItems = itemView.findViewById(R.id.text_view_order_items);
            textViewOrderNote = itemView.findViewById(R.id.text_view_order_note);
            textViewOrderPrice = itemView.findViewById(R.id.text_view_order_price);
            textViewDeliveryType = itemView.findViewById(R.id.text_view_delivery_type);
            textViewDeliveryAddress = itemView.findViewById(R.id.text_view_delivery_address);
        }
    }
} 